package com.weiaibenpao.demo.chislim;

import android.*;
import android.app.Activity;
import android.content.Context;
import android.os.Build;
import android.os.StrictMode;
import android.support.multidex.MultiDex;

import com.luck.picture.lib.permissions.RxPermissions;
import com.orhanobut.logger.AndroidLogAdapter;
import com.orhanobut.logger.FormatStrategy;
import com.orhanobut.logger.Logger;
import com.orhanobut.logger.PrettyFormatStrategy;
import com.squareup.leakcanary.LeakCanary;
import com.squareup.leakcanary.RefWatcher;
import com.umeng.socialize.Config;
import com.umeng.socialize.PlatformConfig;
import com.umeng.socialize.UMShareAPI;
import com.umeng.socialize.common.QueuedWork;
import com.weiaibenpao.demo.chislim.api.Url;
import com.weiaibenpao.demo.chislim.db.entities.MyObjectBox;
import com.weiaibenpao.demo.chislim.retrofit.NetWorkManager;
import com.weiaibenpao.demo.chislim.tts.util.SpeechUtil;
import com.weiaibenpao.demo.chislim.utils.Utils;

import cn.jpush.android.api.JPushInterface;
import io.objectbox.BoxStore;
import io.objectbox.android.AndroidObjectBrowser;
import io.reactivex.functions.Consumer;
import weiaibenpao.com.library.LibraryApplication;
import weiaibenpao.com.library.pedometer.ApplicationModule;


/**
 * Created by Administrator on 2017/12/4.
 */

public class MyApplication extends LibraryApplication {
    private static MyApplication INSTANCE;
    public static MyApplication getInstance() {
        return INSTANCE;
    }
    {
        //微信 appid appsecret   5e93dc03527af1ba094dd7c5322ac985
        PlatformConfig.setWeixin("wx221705f86c71d1bf", "5e93dc03527af1ba094dd7c5322ac985");

        //新浪微博 appkey appsecret   "http://sns.whalecloud.com"
        PlatformConfig.setSinaWeibo("1959713611","e62e0d810039469a8852ba366afdb0bc","http://www.chislim.com");

        // QQ和Qzone appid appkey
        PlatformConfig.setQQZone("1105642894", "3cvNf8EoHe17VQ93");

        //安卓7.0访问相机失败
        if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.N) {
            StrictMode.VmPolicy.Builder builder = new StrictMode.VmPolicy.Builder();
            StrictMode.setVmPolicy(builder.build());
        }

    }
    public static RefWatcher getRefWatcher(Context context) {
        MyApplication application = (MyApplication) context.getApplicationContext();
        return application.refWatcher;//LeakCanary默认自动监视Activity的泄露 其他对象使用这个refWatcher
    }

    private RefWatcher refWatcher;
    private BoxStore boxStore;

    @Override
    public void onCreate() {
        super.onCreate();
        if(!Utils.getProcessName(this).equals(getPackageName())){
            return;
        }
        INSTANCE = this;
        initLogger();
        //初始化友盟
        //开启debug模式，方便定位错误，具体错误检查方式可以查看http://dev.umeng.com/social/android/quick-integration的报错必看，正式发布，请关闭该模式
        Config.DEBUG = true;
        QueuedWork.isUseThreadPool = false;
        UMShareAPI.get(this);
        /**
         * 网络请求初始化
         */
        NetWorkManager.init(Url.BASE_URL,0,this);
        NetWorkManager.setOpenApiException(true);



        ApplicationModule.initSingleton().onCreateMainProcess();
        JPushInterface.setDebugMode(BuildConfig.DEBUG);
        JPushInterface.init(this);
        refWatcher = LeakCanary.install(this);
        boxStore = MyObjectBox.builder().androidContext(this).build();//MyObjectBox必须先创建了entity后由ObjectBox自动生成
        if (BuildConfig.DEBUG) {
            new AndroidObjectBrowser(boxStore).start(this);
        }
    }

    private void initLogger() {
        FormatStrategy formatStrategy = PrettyFormatStrategy.newBuilder()
                .showThreadInfo(false)  // (Optional) Whether to show thread info or not. Default true
                .methodCount(0)         // (Optional) How many method line to show. Default 2
                .methodOffset(7)        // (Optional) Hides internal method calls up to offset. Default 5
//                .tag("My custom tag")   // (Optional) Global tag for every log. Default PRETTY_LOGGER
                .build();

        Logger.addLogAdapter(new AndroidLogAdapter(formatStrategy){
            @Override
            public boolean isLoggable(int priority, String tag) {
                return BuildConfig.DEBUG;
            }
        });
    }

    public BoxStore getBoxStore() {
        return boxStore;
    }

    @Override
    protected void attachBaseContext(Context base) {
        super.attachBaseContext(base);
        MultiDex.install(this);
    }
}
